﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Model;


namespace WhatIfBusiness
{
    public class Workspace{
        public Workspace(){}
        private WhatIfProject _whatIfProject = WhatIfProject.WhatIfProjectInstance;

        public WhatIfProject WhatIfProject{
            get { return _whatIfProject; }
            set { _whatIfProject = value; }
        }
        private static List<string> _hideColumns = new List<string>();
        private readonly static List<string> _allColumns = new List<string>() { "What If 提问", "原因", "后果", "现有措施", "S", "L"
        ,"RR","建议类别","建议编号","建议","负责人","备注"};
        public static List<string> AllColumns
        {
            get { return _allColumns; }
        }

        public List<string> GetHideColumns()
        {
            return _hideColumns;
        }

        public List<string> GetAllColumns()
        {
            return _allColumns;
        }

        public event Action<List<string>> ColumnsChanged;
        public event Action<Dictionary<string, string>> PidListChanged;

        public void OnColumnsChanged(List<string> s)
        {
            if (ColumnsChanged != null)
            {
                ColumnsChanged(s);
            }
        }

        public void OnPidListChanged(Dictionary<string, string> d)
        {
            if (PidListChanged != null)
            {
                PidListChanged(d);
            }
        }

        public void HideColumns(List<string> cols)
        {
            _hideColumns.Clear();
            foreach (string s in _allColumns)
            {
                if (!cols.Contains(s))
                {
                    _hideColumns.Add(s);
                }
            }
            OnColumnsChanged(_hideColumns);
        }

        public void InitProjectInstance(string comName, string proName, string proLoc, string TecUni, string type)
        {
            NewProjectDetails projectDetails = new NewProjectDetails(comName,proName,proLoc,TecUni,type);
            _whatIfProject.ProjectDetails = projectDetails;
        }

        public WhatIfWorkNode GetWorkNodeByNodeName(string name){
            foreach (WhatIfWorkNode node in _whatIfProject.WhatIfNodeList){
                if (node.NodeName.Equals(name)){
                    return node;
                }
            }
            return null;
        }

        public void AddNewWorkNode(WhatIfWorkNode workNode){
            _whatIfProject.WhatIfNodeList.Add(workNode);
        }

        public void SaveProject(string fileName){
            BinaryFormatter binaryFormtter = new BinaryFormatter();
            Stream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
            binaryFormtter.Serialize(fstream,_whatIfProject);
            fstream.Close();
        }

        public WhatIfProject OpenProject(string fileName)
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            Stream fstream = File.OpenRead(fileName);
            _whatIfProject = (WhatIfProject)binaryFormatter.Deserialize(fstream);
            fstream.Close();
            return _whatIfProject;
        }

        public List<string> GetPidNumList()
        {
            List<string> res = new List<string>();
            foreach (PID pid in _whatIfProject.PidList)
            {
                res.Add(pid.DrawingNum);
            }
            return res;
        }

        public List<string> GetPsiList()
        {
            return _whatIfProject.PsiList;
        }

        public void SavePsiList(List<string> list)
        {
            _whatIfProject.PsiList.Clear();
            _whatIfProject.PsiList = list;
        }

        public bool HasNode(string name)
        {
            foreach (WhatIfWorkNode workNode in _whatIfProject.WhatIfNodeList)
            {
                if (workNode.NodeName.Equals(name))
                {
                    return true;
                }
            }
            return false;
        }

        public string[,] GetRiskMatrix()
        {
            return _whatIfProject.RiskMatrix.RmMatrix;
        }

        public List<WhatIfWorkNodeItem> GetAllWorkNodeItemsByNodeName(string name)
        {
            foreach (WhatIfWorkNode node in _whatIfProject.WhatIfNodeList)
            {
                if (node.NodeName.Equals(name))
                {
                    return node.WhatIfWorkNodeItemList;
                }
            }
            return null;
        }

        public string CalculateMatrix(string s, string l)
        {
            int ss = Int32.Parse(s) - 1;
            int ll = Int32.Parse(l) - 1;
            if (ss < 0 || ss > 4 || ll < 0 || ll > 4)
                return string.Empty;
            return _whatIfProject.RiskMatrix.RmMatrix[ll, ss];
        }

        public int GetAdviceCountByNodeName(string name)
        {
            int count = 0;
            WhatIfWorkNode node = GetWorkNodeByNodeName(name);
            if (node == null)
            {
                return 0;
            }
            foreach (WhatIfWorkNodeItem item in node.WhatIfWorkNodeItemList)
            {
                if (item.Advice != null)
                {
                    count++;
                }
            }
            return count;
        }

        private static Dictionary<string, List<WhatIfAskItem>> _askItemList;
        private static Dictionary<string, List<WhatIfAskItem>> _hazardList;
        private static List<string> _titleList = new List<string>();

        public Dictionary<string, List<WhatIfAskItem>> HazardList
        {
            get { return _hazardList; }
            set { _hazardList = value; }
        }
        public  Dictionary<string, List<WhatIfAskItem>> AskItemList{
            get { return _askItemList; }
            set { _askItemList = value; }
        }

        public  List<string> TitleList{
            get { return _titleList; }
            set { _titleList = value; }
        }

        static Workspace(){
            _askItemList = DataAccess.CSVHelper.GetAskItems2("./Config/WhatIf检查表.xls");
            _hazardList = DataAccess.CSVHelper.GetAskItems2("./Config/典型工艺危害.xls");
            _titleList = DataAccess.CSVHelper.GetTitleList();
        }
    }
}
